namespace System.Xml.Serialization.Internal
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    internal static class XmlTypeHelper
    {
        public static string GetTypeName(this Type type)
        {
            StringBuilder name = new StringBuilder();
            if (type != null)
            {
                if (type.IsGenericType)
                {
                    IEnumerable<Type> arguments = type.GetGenericArguments();
                    IEnumerator<Type> enumerator = arguments.GetEnumerator();
                    if (enumerator.MoveNext())
                    {
                        string serviceName = type.Name;
                        int index = type.Name.IndexOf('`');
                        if (index != -1)
                        {
                            serviceName = type.Name.Remove(index);
                        }
                        name.Append(serviceName);
                        name.Append("<");
                        name.Append(GetTypeName(enumerator.Current));
                        while (enumerator.MoveNext())
                        {
                            name.Append(",");
                            name.Append(GetTypeName(enumerator.Current));
                        }
                        name.Append(">");
                    }
                    return name.ToString();
                }
                name.Append(type.Name);
            }
            return name.ToString();
        }
    }
}

